<?php

function getOrderStates($state, $color, $is_seller) {
    $arr = array(
//        0 => array(
//            "state" => 0,
//            "tip" => "待付款"
//        ),
        1 => array(
            "state" => 1,
            "tip" => "待发货",
            "tip_seller" => "待发货",
            "color" => "#794b12",
            "color_seller" => "#794b12",
        ),
        2 => array(
            "state" => 2,
            "tip" => "卖家已发货",
            "tip_seller" => "卖家已发货",
            "color" => "#333",
            "color_seller" => "#333",
        ),
        3 => array(
            "state" => 3,
            "tip" => "仓库已收货",
            "tip_seller" => "已完成",
            "color" => "#333",
            "color_seller" => "#794b12",
        ),
        4 => array(
            "state" => 4,
            "tip" => "仓库已发货",
            "tip_seller" => "已完成",
            "color" => "#333",
            "color_seller" => "#794b12",
        ),
        5 => array(
            "state" => 5,
            "tip" => "仓库已确认到国内",
            "tip_seller" => "已完成",
            "color" => "#333",
            "color_seller" => "#794b12",
        ),
        6 => array(
            "state" => 100,
            "tip" => "已完成", //确认收货后，代表已完成
            "tip_seller" => "已完成",
            "color" => "#794b12",
            "color_seller" => "#794b12",
        ),
        7 => array(
            "state" => -1,
            "tip" => "已取消",
            "tip_seller" => "已取消",
            "color" => "#aaa",
            "color_seller" => "#794b12",
        ),
    );
    if ($state != '') {
        foreach ($arr as $v) {
            if ($state == $v['state']) {

                if ($is_seller == 1) {
                    if ($color == 1) {
                        return "<span style='color:" . $v['color_seller'] . "'>" . $v['tip_seller'] . "</span>";
                    } else {
                        return $v['tip_seller'];
                    }
                } else {
                    if ($color == 1) {
                        return "<span style='color:" . $v['color'] . "'>" . $v['tip'] . "</span>";
                    } else {
                        return $v['tip'];
                    }
                }

                break;
            }
        }
    } else {
        return $arr;
    }
}

function getApiUrl($detail) {
    return C("SITE_URL") . $detail['control'] . "/" . $detail['mod'];
}

function getApiInfo($postArr) {
    $control = strtolower(CONTROLLER_NAME);
    $mod = strtolower(ACTION_NAME);
    $info = M("api")->field("parameters")->where("control = '" . $control . "' AND `mod` = '" . $mod . "'")->find();
    $parameters = json_decode($info['parameters'], true);
//  print_r($parameters);
    $rs = "";
    foreach ($parameters as $v) {
        $field = $v['paras'];
//        echo $filed."<hr>";
        $value = $postArr[$field];
//         echo $field."<hr>";
        //检测是否为空
        if ($v['is_null'] != 1 && $value == '') {
            $rs = $v['descriptions'] . "不能为空";
            break;
        }
        //检测验证类型
        if ($v['check_types'] != '' && $v['is_null'] != 1) {
            $rs_type = getApiCheckType($value, $v['check_types'], $v['descriptions']);
            if ($rs_type != '') {
                $rs = $rs_type;
                break;
            }
        }
    }
    return $rs;
}

function getApiCheckType($value, $check_type, $descriptions) {
    if ($check_type == 'phone') {
        $pattern = "/^1[3|4|5|7|8][0-9]{9}$/";
    }
    if ($check_type == 'interPhone') {
        $pattern = "/(?:\(?[0\+]?\d{1,3}\)?)[\s-]?(?:0|\d{1,4})[\s-]?(?:(?:13\d{9})|(?:\d{7,8}))/";
    }
    if ($check_type == 'email') {
        $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
    }
    if ($check_type == 'phone_email') {
        if (!preg_match("/^1[3|4|5|7|8][0-9]{9}$/", $value) && !preg_match("/^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/", $value)) {
            return $descriptions . "格式不正确";
        }
        return "";
    }
    if ($check_type == 'interPhone_email') {
        if (!preg_match("/(?:\(?[0\+]?\d{1,3}\)?)[\s-]?(?:0|\d{1,4})[\s-]?(?:(?:13\d{9})|(?:\d{7,8}))/", $value) && !preg_match("/^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/", $value)) {
            return $descriptions . "格式不正确";
        }
        return "";
    }
    if ($check_type == 'number') {
        $pattern = "/^[0-9]+(.[0-9]{1,2})?$/";
    }
    if ($check_type == 'positive_integer') {
        $pattern = "/^[1-9]\d*$/";
    }
//    if($check_type == 'email'){
//          echo $value;
//          echo preg_match($pattern, $value);
//    }


    if (preg_match($pattern, $value) == 0) {
        return $descriptions . "格式不正确";
    }
}

function getParamValues($arr, $fields) {
    $rs = array();
    $fieldsArr = explode(",", $fields);
    foreach ($arr as $k => $v) {
        if (in_array($k, $fieldsArr)) {
            $rs[$k] = trim($v);
        }
    }
    return $rs;
}

function checkRegSubmitCode($name, $code_mobile) {
    $detail_short = getSendCode($name, 'reg_code');
    $rand = $detail_short['rand'];
//             echo $rand."<hr>".$code_mobile;
    if ($code_mobile == 'sucaihuo') {
        $detail_short['result'] = "ok";
    } else {
//        file_put_contents("aa_text.txt", $rand."|".$code_mobile);
        if ($rand != $code_mobile || $code_mobile == '') {
            $detail_short['result'] = "验证码有误";
        } else {
            $detail_short['result'] = "ok";
        }
    }

    return $detail_short;
}

function getFileBei($id) {
    return intval($id / 100) . "/";
}

function getUnits($code) {
    $arr = array(
        0 => array(
            "code" => "dollar",
            "value" => "$",
            "name" => "美国"
        ),
        1 => array(
            "code" => "common",
            "value" => "common",
            "name" => "非美国"
        )
    );
    if ($code) {
        foreach ($arr as $v) {
            if ($v['code'] == $code) {
                $chose[] = $v;
                break;
            }
        }
        return $chose;
    } else {
        return $arr;
    }
}

function getGoodsSize($size_id) {
    if ($size_id > 0) {
        $info = M("goods_size")->field("name")->where("id = " . $size_id . "")->find();
        return $info['name'];
    }
}

function getGoodsSizeNames($size_ids) {
    if ($size_ids) {
        $sizes = M("goods_size")->field("name,id")->where("id in (" . $size_ids . ") AND is_show =1")->order("ord ASC")->select();
    }
    return $sizes;
}

function getCatCost($cat_id, $cat_cost_carry) {
    $cat_cost_carries = json_decode($cat_cost_carry, true);
    $rs = 0;
    if ($cat_id > 0) {
        foreach ($cat_cost_carries as $v) {
            if ($v['cat_id'] == $cat_id && $v['costs'] != '') {
                $rs = $v['costs'];
                break;
            }
        }
    }
    return $rs;
}

function getHandPrice($percent_hand, $price) {
    $rs = 0;
    if ($percent_hand > 0) {
        $rs = $price * $percent_hand / 100;
    }
    return $rs;
}

function getConsumeTax($percent_consume, $price) {
    $rs = 0;
    if ($percent_consume > 0) {
        $rs = $price * $percent_consume / 100;
    }
    return $rs;
}

function getShipments($code, $field = 'name') {
    $arr = array(
        0 => array(
            "code" => "USA",
            "name" => "美国"
        ),
        1 => array(
            "code" => "Europe",
            "name" => "欧洲"
        ),
    );
    if ($code) {
        foreach ($arr as $v) {
            if ($code == $v['code']) {
                return $v['name'];
                break;
            }
        }
    } else {
        return $arr;
    }
}

function getReceivingWay($code) {
    $arr = array(
        0 => array(
            "code" => "direct_mail",
            "name" => "直邮"
        ),
        1 => array(
            "code" => "gull",
            "name" => "海鸥"
        ),
    );
    if ($code) {
        foreach ($arr as $v) {
            if ($code == $v['code']) {
                return $v['name'];
                break;
            }
        }
    } else {
        return $arr;
    }
}

function getImplode($strs) {
    $arr = explode(",", $strs);
    $rs = "";
    foreach ($arr as $v) {
        $rs .= "'" . $v . "'" . ",";
    }
    return substr($rs, 0, -1);
}

function getGoodsLogo($goods_id) {
    $detail = M("goods")->field("logo")->where("id = " . $goods_id . "")->find();
    return $detail['logo'];
}

function getRmbTransfer($price, $unit_id) {
    if ($unit_id > 0 && $price != '') {
        $info = M("goods_unit")->field("rate")->where("id  = " . $unit_id . "")->find();
        return $info['rate'] * $price;
    }
}

function getPayType($type) {
    $arr = getPayTypes();
    if ($type != '') {
        foreach ($arr as $v) {
            if ($type == $v['code']) {
                return $v['tip'];
                break;
            }
        }
    } else {
        return '';
    }
}

function getPayTypes() {
    $arr = array(
        0 => array(
            "code" => 'paypal',
            "tip" => "paypal"
        ),
        1 => array(
            "code" => 'bank',
            "tip" => "银联"
        ),
    );
    return $arr;
}

function getSellerId($goods_id) {
    if ($goods_id > 0) {
        $detail = M("goods")->field("uid")->where("id = " . $goods_id . "")->find();
        return $detail['uid'];
    }
}

function getAreaTime($area_checktime) {
    $now = time();
    $days_check = C("days_check") * 3600 * 24;
    $other_days = ceil(($days_check - ($now - $area_checktime)) / (3600 * 24));
    $rs = ($other_days > 0) ? $other_days : 0;
    return $rs;
}

function getExpressCompany($id) {
    if ($id > 0) {
        return getSingleField($id, 'express', 'name');
    }
}

function getPics($pics) {
    if ($pics) {
        $picsArr = explode(",", $pics);
        $strs = "";
        foreach ($picsArr as $v) {
            $img = __APP__ . "/" . $v;
            $strs .= "<p><a class='fancybox_img' href='" . $img . "'><img class='admin_img' src='" . $img . "'></p>";
        }
        return $strs;
    }
}

function getSellerState($state) {
    if ($state == 1) {
        $rs = "<span class='green'>已通过</span>";
    } elseif ($state == -1) {
        $rs = "<span class='red'>已拒绝</span>";
    } else {
        $rs = "<span style='color:#ccc'>待审核</span>";
    }
    return $rs;
}

function getCountryName($id) {
    return getSingleField($id, 'goods_country', 'name');
}

function getCityName($id) {
    return getSingleField($id, 'goods_city', 'name');
}

function getPicImg($pic_url) {
    if ($pic_url) {
        $img = __APP__ . "/" . $pic_url;
        return "<a class='fancybox_img' href='" . $img . "'><img class='admin_img' src='" . __APP__ . "/" . $pic_url . "'></a>";
    }
}

function getBaiduMapJson($content) {
    $content = str_replace("[code]", "<pre><code class='html'>", $content);
    $content = str_replace("[code='html']", "<pre><code class='html'>", $content);
    $content = str_replace("[code='js']", "<pre><code class='js'>", $content);
    $content = str_replace("[code='css']", "<pre><code class='css'>", $content);
    $content = str_replace("[code='php']", "<pre><code class='php'>", $content);
    $content = str_replace("[/code]", "</code></pre>", $content);
    $tagsArr = array("code", "pre", "em", "b", "strong", "p", "br", "a", "span", "dd", "dl", "dt", "table", "tr", "th", "td", "thead", "tbody", "img",
        "h1", "h2", "h3", "h4", "h5", "h5", "div", "i", "dfn", "u", "ins", "strike", "s", "del", "font", "hr", "center", "caption", "html", "body", "ul", "li", "ol", "select", "small", "input", "textarea"
    );
    $tags = "";
    foreach ($tagsArr as $v) {
        $tags .="<" . $v . ">";
    }
    return strip_tags($content, $tags);
}

function isRepreatName($name, $table, $id) {
    if ($name && $table) {
        if ($id > 0) {
            $info = M($table)->field("id")->where("name ='" . $name . "' AND id != " . $id . "")->find();
        } else {
            $info = M($table)->field("id")->where("name ='" . $name . "'")->find();
        }
    }
    return $info;
}

function getLocation($longitude, $latitude) {
    $rs = "";
    if ($longitude && $latitude) {
        $url = "http://api.map.baidu.com/geocoder/v2/?ak=uPfy5gxOijbsZqkjK1twamZV&callback=renderReverse&location=" . $longitude . "," . $latitude . "&output=xml&pois=1";
//        echo $url;
        $xml = getUrlJson($url);
        $xmlArr = xmlToArray2($xml);
        $rs = $xmlArr['result']['addressComponent'];
    }
    return $rs;
}

function getLocationInfo($latitude, $longitude) {
    $info = getLocation($longitude, $latitude);
    return "省份：" . $info['province'] . " 州/区：" . $info['city'];
}

function xmlToArray2($xml) {
    // 将XML转为array
    $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    return $array_data;
}

function getNationOtherDays($order_id) {

    $order_info = M("order")->field("shipments_code,delivery_time,state")->where("id = " . $order_id . "")->find();
    if ($order_info['state'] == 4 && $order_info['delivery_time'] > 0) {
        $days = 7;
        $other_days = ceil((time() - $order_info['delivery_time']) / 3600 / 24);
        if ($order_info['shipments_code'] == 'USA') {
            $days = 15;
        }
        if ($other_days >= $days) {
            return "（<span class='red'>" . $other_days . "</span>天未到国内）";
        }
    }
}

function getCatAttrs($cat_id) {
    $cat_info = M("goods_cats")->field("attr_ids")->where("id = " . $cat_id . "")->find();
    $attr_ids = $cat_info['attr_ids'];
    if ($attr_ids) {
        $attrs = M('goods_attr')->field("name,pid,id")->where("id in (" . $attr_ids . ") AND is_show =1")->order("ord ASC")->select();
        foreach ($attrs as $v) {
            if ($v['pid'] > 0) {
                $pids[] = $v['pid'];
            }
        }
        $pids = array_filter(array_unique($pids));
        $attrs_top = M('goods_attr')->field("name,id")->where("id in (" . implode(",", $pids) . ")")->order("ord ASC")->select();
        foreach ($attrs_top as $k => $v) {
            foreach ($attrs as $v2) {
                if ($v['id'] == $v2['pid']) {
                    $attrs_top[$k]['sub'][] = $v2;
                }
            }
        }
    }
    return $attrs_top;
}

function getWaterFontSize($width) {
    $font_size = 20;
    if ($width >= 500) {
        $font_size = 24;
    } elseif ($width >= 700) {
        $font_size = 28;
    } elseif ($width >= 900) {
        $font_size = 32;
    }
    return $font_size;
}

function getWaterImg($width) {
    $water_img = 'Public/images/water_bg.png';
    if ($width <= 500) {
        $water_img = "Public/images/water_bg_500.png";
    } elseif ($width <= 800) {
        $water_img = "Public/images/water_bg_800.png";
    }
    return $water_img;
}

function getExpressTypes($type) {
    $rs = "<span style='color:green'>国际</span>";
    if ($type == 1) {
        $rs = "国内";
    } elseif ($type == 2) {
        $rs = "<span style='color:red'>转运</span>";
    }
    return $rs;
}

function getHeti($detail) {
    $pics = explode(",", $detail['pics']);
    $heti = $detail['logo'];
    if (count($pics) >= 2) {
        $heti = "uploads/heti/" . $detail['id'] . ".jpg";
    }
    return $heti;
}

function getHetiImg($detail) {
    $img = getHeti($detail);
    if (file_exists($img)) {

        return "<a class='fancybox_img' href=" . __APP__ . "/" . $img . "><img src=" . getLogo($img, 100, 100) . "></a>";
    }
}

function getNewWeixinPics($pics, $goods_id,$pics_old) {
    $pic_old = "uploads/heti/" . $goods_id . ".jpg";
    if($pics_old && $pics == $pics_old && file_exists($pic_old)){
        return $pic_old;
    }
  
    if (file_exists($pic_old)) {
        unlink($pic_old);
    }
    $pics_merge = explode(",", $pics);

    foreach ($pics_merge as $k => $v) {
        $logo_500 = getLogoWeixin($v, 500, 0, 1);
//         print_r($logo_500);
        $pics_merge[$k] = C("app_path") . $logo_500;
    }
//   print_r($pics_merge);exit;
    $merge_pics = merge_pics($pics_merge, $goods_id);
    getLogoWeixin($merge_pics, 100, 100, 1);
    return $merge_pics;
}
function get_paypay_currency($unit_name){
    $rs = "USD";
    if($unit_name == '€'){
        $rs = "EUR";
    }
    return $rs;
}
?>